package com.fingard.dsp.bank.directbank.icbc04;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.directbank.icbc04.interfacelist.ICBC04TransCode;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;

import java.util.Date;

public class ICBC04Contract extends ICBC04Base implements IContract {
    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail(p_req);

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "");
            if (actItem == null) {
                throw new Exception("未找到账号[" + p_req.contractDetail.recAct + "]对应的商户配置信息!");
            }

            StringBuilder signBuilder = new StringBuilder();
            Date now = new Date();
            if ("01".equals(p_req.contractDetail.step)) {
                signBuilder.append(ICBC04TransCode.deal_9108Cntr_step01_71351);
                signBuilder.append("6002000293000010");
                signBuilder.append(String.format("%16s", p_req.reqHeader.transSeqID));//商户交易流水号
                signBuilder.append(String.format("%12s", actItem.merchantId));//商户号
                signBuilder.append(" 1");
                signBuilder.append(String.format("%19s", p_req.contractDetail.payAct));//银行卡号
                signBuilder.append(String.format("%11s", p_req.contractDetail.cellPhone));//手机号
                signBuilder.append(String.format("%3s", convertCertTypeToBank(p_req.contractDetail.certType)));
                signBuilder.append(String.format("%40s", p_req.contractDetail.certNum));
                signBuilder.append(leftFill(p_req.contractDetail.payName, 60, " "));
                signBuilder.append(String.format("%6s", p_req.contractDetail.expiredDate));
                signBuilder.append(String.format("%3s", p_req.contractDetail.cvv2));
                signBuilder.append("001");
                signBuilder.append(String.format("%17s", "0"));
                signBuilder.append("000");
            } else if ("02".equals(p_req.contractDetail.step)) {
                signBuilder.append(ICBC04TransCode.deal_9108Cntr_step02_71352);
                signBuilder.append("6002000293000010");
                signBuilder.append(String.format("%16s", p_req.reqHeader.transSeqID));//商户交易流水号
                signBuilder.append(String.format("%12s", actItem.merchantId));//商户号
                signBuilder.append(String.format("%19s", p_req.contractDetail.payAct));//银行卡号
                signBuilder.append(String.format("%11s", p_req.contractDetail.cellPhone));//手机号
                signBuilder.append(String.format("%3s", convertCertTypeToBank(p_req.contractDetail.certType)));
                signBuilder.append(String.format("%40s", p_req.contractDetail.certNum));
                signBuilder.append(leftFill(p_req.contractDetail.payName, 60, " "));
                signBuilder.append(String.format("%6s", p_req.contractDetail.expiredDate));
                signBuilder.append(String.format("%3s", p_req.contractDetail.cvv2));
                signBuilder.append(String.format("%6s", p_req.contractDetail.smsCode));
                signBuilder.append(String.format("%16s", p_req.contractDetail.token));
                signBuilder.append("000");
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未能识别的step：" + p_req.contractDetail.step;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }

            String mac = mac(signBuilder.toString());
            WriteBankLogLn("mac:" + mac);

            StringBuilder tmpSendRequest = new StringBuilder();
            tmpSendRequest.append(String.format("%10s", "224"));
            tmpSendRequest.append(signBuilder);
            tmpSendRequest.append(leftFill(mac, 8, " "));

            String[] tmpStrRet = sendToBank(tmpSendRequest.toString());

            if (tmpStrRet[0].length() == 0) {
                tmpRetDetail.respCode = "0";
                tmpRetDetail.respInfo = new String(tmpStrRet[1].getBytes(getCharset()), 5, 60, getCharset()).trim();
                String tmpRespCode = new String(tmpStrRet[1].getBytes(getCharset()), 0, 5, getCharset()).trim();
                if("01".equals(p_req.contractDetail.step)) {
                    if (!"0000".equals(tmpRespCode)) {
                        tmpRetDetail.respCode = "-1";
                    }
                    tmpRetDetail.token = new String(tmpStrRet[1].getBytes(getCharset()), 65, 16, getCharset()).trim();
                } else {
                    if ("0000".equals(tmpRespCode)) {
                        tmpRetDetail.signState = ContractState.SUCCESS;
                    } else {
                        tmpRetDetail.signState = ContractState.FAIL;
                    }
                }
                tmpRetDetail.signConfirmType = "2";
                tmpRetDetail.unifiedCode = getBankConfig().getMapValueByKey("contractMapping", tmpRespCode);
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        RetAtsContract retAts = new RetAtsContract(p_reqQry.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail(p_reqQry);
        tmpRetDetail.respCode = "0";
        tmpRetDetail.respInfo ="未签约";
        tmpRetDetail.signState = ContractState.UNSIGNED;
        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}